<?php

namespace App\Admin\Controllers;

use App\Models\ProjectModel;
use App\Admin\Models\UserModel;
use App\Admin\Models\GeoModel;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

use Illuminate\Http\Request;
class GeoController extends Controller
{
    use ModelForm;
    public function index(){
        return Admin::content(function (Content $content) {
            
            $content->header('环境数据');
            $content->description('');
//            $method = isset($_GET['method'])?$_GET['method']:0;
            $content->body($this->grid());
        });
        
    }
    public function create(Request $request){
        $this->request = $request;
        return Admin::content(function (Content $content) {

            $content->header('新增数据');
            $content->description('');

            $content->body($this->form());
        });
    }
    
    
    public function edit($id,Request $request)
    {
        $this->request = $request;
        return Admin::content(function (Content $content) use ($id) {

            $content->header('环境数据');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }    
//    public function store(Request $request){
//                echo $request->get('latitude');
//                echo "========";
//                echo $request->get('longitude');exit;
//    }    
   
    public function grid()
    {
//        $this->_method=$method;
        return Admin::grid(GeoModel::class, function (Grid $grid) {
            $grid->resource('admin/geo');
//            $grid->id('ID')->sortable();
            $grid->column('title','监测名称')->sortable();
            $grid->column('operatorid','处理人')->value(function($userId){
                $oManager = new ProjectModel();
//                var_dump($oManager->getManager($userId));
                $oM = $oManager->getManager($userId);
//                var_dump($oM);
                return $oM->name;
            });
            $grid->column('assignerid','项目经理')->value(function($userId){
                $oManager = new ProjectModel();
//                var_dump($oManager->getManager($userId));
                $oM = $oManager->getManager($userId);
//                var_dump($oM);
                return $oM->name;
            });
            $grid->column('createdate','创建时间');
//            $grid->column('enddate','结束时间');

//            $grid->created_at();
//            $grid->updated_at();
            
            $grid->filter(function ($filter) {
    
                  // 设置created_at字段的范围查询
                     $filter->between('createdate', '创建时间')->datetime();
                     $filter->disableIdFilter();
             });
//             var_dump($this->_method);
//             $method = $this->_method; 
//             $grid->model()->where('mode','=',$method); 
//             $aMe = Admin::user();
//             var_dump($aMe->id);exit;
                    
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableBatchDeletion();
//            $grid->CreateButton('申请');
                    
//            $grid->disableActions();
        });
    }
     
     protected function form()
    {
        return Admin::form(GeoModel::class, function (Form $form) {
//            $request = $this->request;
            $id = Admin::user()->id;
//            $form->mapgetAssets();
             $oP = new ProjectModel();
             $aPs = $oP->getManagerNames($id);
             $aJ = $oP->getProjectNames();
            $form->text('title','监控名称')->rules('required');     
            $form->select('operatorid','处理人')->options($aPs); 
            $form->text('latitude','纬度');          
            $form->text('longitude','经度');          
            $form->datetime('createdate','创建时间')->datetime();
            $form->select('projectid','项目名称')->options($aJ);
            $form->select('assignerid', '项目经理')->options($aPs);
            $form->map('latitude', 'longitude','位置')->useTencentMap();
//            $ext = $request->file('filename')->getClientOriginalExtension();
//            var_dump(public_path('uploads\image'));

            $form->file('filename','详细资料')->rules('mimes:doc,jpg,png,jpeg,docx,txt,cvs,xlsx,pdf');
//            $form->label('')
//            $form->setAction('/admin/geo/store');

            $form->model()->timestamps = false;
//            $form->ignore(array('latitude','longitude'));
            

//            $form->display('created_at', 'Created At');
//            $form->display('updated_at', 'Updated At');
            
            

        });
    }
    
}
